package service

import (
	"choujiang/common/models"
	"choujiang/common/db"
	"fmt"
	"choujiang/common/utils"
)

func GetPromoteGame(gameId string, userId, isPay int64) []model.Game {
	box := new(model.Box)
	_, err := db.MysqlEngine.Where("name = ?", gameId).Get(box)
	boxGames := make([]model.Game, 0)
	db.MysqlEngine.Join("INNER", "game_box", "game_box.game_id = game.id").Where("game_box.box_id=? and game.special=?", box.Id, 0).Find(&boxGames)
	boxIds := []int64{}
	for i := 0; i < len(boxGames); i++ {
		boxIds = append(boxIds, boxGames[i].Id)
	}
	games := make([]model.Game, 0)
	err = db.MysqlEngine.Join("INNER", "user_games", "game.id = user_games.game_id").Where("user_games.user_id = ? and game.status = ?", userId, 1).In("game.id", boxIds).Desc("game.order").Find(&games)
	if err != nil {
		fmt.Printf("GetPromoteGame err : %s", err)
	}
	for i := 0; i < len(games); i++ {
		games[i].CateName = GetGameCategory(games[i])
		games[i].Earning = CalculateEarning(games[i].GameId, userId)
		games[i].PromoteUser = CalculatePromote(games[i].GameId, userId)
		games[i].PromoteNumber = len(games[i].PromoteUser)
		games[i].Icon = utils.AddPrefix(games[i].Icon)
		games[i].InviterEarn = utils.Decimal(games[i].Price * 0.3)
		games[i].PlayerEarn = utils.Decimal(games[i].Price * 0.1)
		games[i].Played = db.Mgo.UserIdValidate(userId, games[i].GameId)
	}
	if isPay == 0 { //未付费
		unPayGames := make([]model.Game, 0)
		ids := []int64{}
		for i := 0; i < len(games); i++ {
			ids = append(ids, games[i].Id)
		}
		err := db.MysqlEngine.In("game.id", boxIds).NotIn("game.id", ids).Where("status=?", 1).Find(&unPayGames)
		if err != nil {
			fmt.Printf("GetPromoteGame err : %s", err)
		}
		for i := 0; i < len(unPayGames); i++ {
			unPayGames[i].CateName = GetGameCategory(unPayGames[i])
			unPayGames[i].Icon = utils.AddPrefix(unPayGames[i].Icon)
			unPayGames[i].InviterEarn = utils.Decimal(unPayGames[i].Price * 0.3)
			unPayGames[i].PlayerEarn = utils.Decimal(unPayGames[i].Price * 0.1)
			unPayGames[i].Played = db.Mgo.UserIdValidate(userId, unPayGames[i].GameId)
		}
		return unPayGames
	}
	return games
}

func GetGameCategory(game model.Game) []string {
	reString := []string{}
	categorys := make([]model.Categorys, 0)
	db.MysqlEngine.Join("INNER", "game_categorys", "categorys.id = game_categorys.cid").Where("game_categorys.gid=?", game.Id).Find(&categorys)
	for i := 0; i < len(categorys); i++ {
		reString = append(reString, categorys[i].Name)
	}
	return reString
}

func GetGameTag(game model.Game) []string {
	reString := []string{}
	tags := make([]model.Tags, 0)
	db.MysqlEngine.Join("INNER", "game_tags", "tags.id = game_tags.tid").Where("game_tags.gid=?", game.Id).Find(&tags)
	for i := 0; i < len(tags); i++ {
		reString = append(reString, tags[i].Name)
	}
	return reString
}

func CalculateEarning(gameId string, userId int64) float64 {
	earn := db.Mgo.GetPromoteEarning(gameId, userId)
	fmt.Println("-------------earn", earn)
	if len(earn) > 0 {
		return utils.Decimal(earn[0]["inviterearn"].(float64))
	} else {
		return 0
	}
}

func CalculatePromote(gameId string, userId int64) []string {
	promote := db.Mgo.GetPromoteUser(gameId, userId)
	return promote
}

func GetEarnDetail(user *model.User, pageNo, pageSize int) []db.SdkUserReward {
	logs := db.Mgo.GetEarnLog(user.Id, pageNo, pageSize)
	for i := 0; i < len(logs); i++ {
		logs[i].FormatDate = utils.FormatDate(logs[i].DateTime)
		logs[i].InviterEarn = utils.Decimal(logs[i].InviterEarn)
		logs[i].PlayerEarn = utils.Decimal(logs[i].PlayerEarn)
	}
	return logs
}
